class CreateStatisticsFields < ActiveRecord::Migration
  def self.up
    add_column    :included_questions , :times_asked          , :integer , :default => 0
    add_column    :included_questions , :times_answered       , :integer , :default => 0
    add_column    :included_questions , :correct_answers      , :integer , :default => 0
    add_column    :included_questions , :not_perfect_answers  , :integer , :default => 0
    rename_column :included_questions , :score                , :weight
    change_column :included_questions , :weight               , :float   , {:default => 1, :null => false}
    execute("UPDATE included_questions SET weight = 1.0")
    
    # Derivative attributes
    add_column    :questions          , :times_asked          , :integer , :default => 0
    add_column    :questions          , :times_answered       , :integer , :default => 0
    add_column    :questions          , :correct_answers      , :integer , :default => 0
    add_column    :questions          , :not_perfect_answers  , :integer , :default => 0

    add_column    :questionnaires     , :times_taken          , :integer , :default => 0
    add_column    :questionnaires     , :average_score        , :float   , :default => 1.0
    
    add_column    :users              , :answered_questions   , :integer , :default => 0
    add_column    :users              , :unanswered_questions , :integer , :default => 0
    add_column    :users              , :correct_answers      , :integer , :default => 0
    add_column    :users              , :not_perfect_answers  , :integer , :default => 0
    add_column    :users              , :taken_questionnaires , :integer , :default => 0
    add_column    :users              , :average_score        , :float   , :default => 1.0

    # OTHER: Question maximum time and penalty for wrong answers and unanswered questions
    add_column    :questions          , :max_time             , :integer , :default => 60  # Seconds
    add_column    :questionnaires     , :wrong_answer_penalty , :float   , :default => -1  # Percentage (-1 => score = 0 when random answer)
    add_column    :questionnaires     , :no_answer_penalty    , :float   , :default => 0   # Percentage
    execute("UPDATE answers SET score = 0.0 WHERE score < 0.0")

  end

  def self.down
    remove_column :questionnaires     , :no_answer_penalty
    remove_column :questionnaires     , :wrong_answer_penalty
    remove_column :questions          , :max_time
    
    remove_column :users              , :average_score
    remove_column :users              , :taken_questionnaires
    remove_column :users              , :not_perfect_answers
    remove_column :users              , :correct_answers
    remove_column :users              , :unanswered_questions
    remove_column :users              , :answered_questions

    remove_column :questionnaires     , :average_score
    remove_column :questionnaires     , :times_taken
    
    remove_column :questions          , :not_perfect_answers
    remove_column :questions          , :correct_answers
    remove_column :questions          , :times_answered
    remove_column :questions          , :times_asked

    rename_column :included_questions , :weight, :score
    remove_column :included_questions , :not_perfect_answers
    remove_column :included_questions , :correct_answers
    remove_column :included_questions , :times_answered
    remove_column :included_questions , :times_asked
  end
end
